Page({
    data: {
        name: '',
        phone: '',
        detail: '',
        region: ['陕西省', '西安市', '长安区'],
        addrId: null,
        array: [],  
        index: 0,
        num: null,
        addrDid: null,
        userID: null
    },

    onLoad (query) {
        let data = JSON.parse(query.data); 
        console.log('==============================');
        console.log(query.num);
        console.log(data);
        console.log('==============================');
        if (typeof(query.num) == 'undefined') {
            this.setData({
                num: 2
            })
        } else {
            this.setData({
                num: 1
            })
        }
        console.log(this.data.num);
        let that = this;  
        wx.getStorage({
            key: 'address',
            success: function(res) {
                that.setData({
                    address : res.data
                })
            }
        });
        let regions = [];
        regions[0] = data.addrProvince;
        regions[1] = data.addrCity;
        regions[2] = data.addrArea;
        this.setData({
            name: data.addrNickname,
            phone: data.addrPhone,
            detail: data.addrDetail,
            region: regions,
            addrId: data.addrId,
            addrDid: data.addrDid
        })
        console.log(this.data.addrId);
        wx.request({
            url: 'https://localhost:80/commuity/query',
            method: 'get',
            header: {
                'content-type': 'application/json' 
            },
            success (res) {
                console.log(res.data);
                console.log("----------===");
                let areaArray = [];
                let n = 0;
                for (let i = 0; i < res.data.length; i++)
                {
                    areaArray[i] = res.data[i].cName
                    if (areaArray[i] == data.addrCommuity) {
                        n = i
                    }
                }
                that.setData({
                    array: areaArray,
                    index: n
                })
            }
        });
    },

    formSubmit (e) { 
        const value = e.detail.value;
        console.log(value);
        wx.getStorage({
            key: 'userID',
            success(res) {
                that.setData({
                    userID: res.data
                }) 
            }
        });
        let userId = this.data.userID;
        if (userId == null) {
            userId = getApp().globalData.userID
        }
        let that = this;
        if (value.name && value.phone && value.detail && value.region) {
            let str = /^1\d{10}$/;
            let judge = str.test(value.phone);
            if (!judge) {
                wx.showToast({
                    title: '手机格式错误',
                    icon: 'none',
                    duration: 1000
                })
            } else {
                wx.request({
                    url: 'https://localhost:80/address/update',
                    method: 'post',
                    data: {      
                        addrArea: value.region[2],
                        addrCity: value.region[1],
                        addrCommuity: that.data.array[value.area],
                        addrDetail: value.detail,
                        addrDid: that.data.addrDid,
                        addrId: that.data.addrId,
                        addrNickname: value.name,
                        addrPhone: value.phone,
                        addrProvince: value.region[0],
                        addrState: 1,
                        addrUser: userId,
                        addrZipcode: ""
                    },
                    header: {
                        'content-type': 'application/json' 
                    },
                    success (res) {
                        console.log('--------------------------------');
                        console.log(res);
                        wx.showToast({
                            title: '地址保存成功',
                            icon: 'success',
                            duration: 1000
                        })
                        console.log(that.data.num);
                        if (that.data.num == 1) {
                            console.log('takeaddress');
                            setTimeout(
                                function () {
                                    wx.navigateTo({
                                        url: '../takeaddress/takeaddress'
                                    })  
                                }, 2000); 
                        } else {
                            console.log('person');
                            setTimeout(
                                function () {
                                    wx.navigateTo({
                                        url: '../alladdress/alladdress'
                                    })  
                                }, 2000);  
                        }
                        console.log('------------------------------');
                    }
                })
            }     
        } else {
            wx.showModal({
                title: '提示',
                content: '请填写完整资料',
                showCancel: false
            })
        }
    },

    deteleaddr () {
        console.log(this.data.addrId);
        let that = this;
        wx.showModal({
            title: '亲，确定删除该地址吗？',
            content: '',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    wx.request({
                        url: 'https://localhost:80/address/delete/' + that.data.addrId,
                        method: 'get',
                        header: {
                            'content-type': 'application/json' 
                        },
                        success (res) {
                            console.log(res);
                            wx.showToast({
                                title: '地址删除成功',
                                icon: 'success',
                                duration: 1000
                            })
                            if (that.data.num == '1') {
                                setTimeout(
                                    function () {
                                        wx.navigateTo({
                                            url: '../takeaddress/takeaddress'
                                        })  
                                    }, 1000);                   
                            } else {
                                setTimeout(
                                    function () {
                                        wx.navigateTo({
                                            url: '../alladdress/alladdress'
                                        })  
                                    }, 1000);               
                            }
                        }
                    })  
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },

    bindPickerChange: function (e) {
        let num = e.detail.value;
        console.log('picker发送选择改变，携带值为', this.data.array[num]);
        this.setData({
            index: e.detail.value
        })
    },

    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region: e.detail.value
        })
    } 
})